package york.Game.Steering.BehaviorTypes;

import java.util.Random;

import york.Game.Transformer;
import york.Game.Vector2D;
import york.Game.Steering.SteeringBehavior;
import york.Game.Unit.SmartUnit;

public class Wander extends SteeringBehavior{
	double WanderRadius;
	double WanderDistance;
	double WanderJitter;
	Vector2D Target;
	
	public Wander(double WR, double WD,double WJ) {
		DebugTarget = new SmartUnit(0);
		DebugTarget.setSize(5);
		DebugTarget.setPosition(new Vector2D(10,10));
		
		generator = new Random();
		WanderRadius = WR;
		WanderDistance = WD;
		WanderJitter = WJ;
	}
	
	public void doWork(SmartUnit arg0,double dt) {
		arg0.setDest(nextLoc(arg0.getPosition(),arg0.heading(dt),dt));
	}
	
	public double RandomClamped() {
		return generator.nextDouble() * 2 - 1;
	}
	
	public Vector2D nextLoc(Vector2D a_Vec,Vector2D Head,double dt) {
		
		Target = new Vector2D(a_Vec.x,a_Vec.y);
		
		Target.add(new Vector2D(RandomClamped() * WanderJitter,
								RandomClamped() * WanderJitter));
		
		Target.normalize();
		
		Target.Multiply(WanderRadius);
		
		Target.add(new Vector2D(WanderDistance,0));
		
		//Convert
		Target = Transformer.Local2World(Target, Head, Head.Perpendicular(), a_Vec);
		
		DebugTarget.setPosition(Target);
		return Target;
	}
	
}
